#include "pch.h"
#include "GRiSceneObject.h"
#include <map>
#include <GGenericInfra/GGiEngineUtil.h>

GRiMaterial* GRiSceneObject::GetOverrideMaterial(std::string submeshName)
{
	auto it = pOverrideMaterial.find(submeshName);
	if (it != pOverrideMaterial.end())
	{
		return it->second;
	}
	return nullptr;
}

void GRiSceneObject::SetMesh(GRiMesh* mesh)
{
	pMesh = mesh;
}

GRiMesh* GRiSceneObject::GetMesh()
{
	return pMesh;
}


//std::map<std::string, std::string> GRiSceneObject::GetOverrideMaterialNames()
//{
//	//std::map<std::string, std::string> ret;
//	//ret.clear();
//	//
//	//for (auto ovrdMat : pOverrideMaterial)
//	//{
//	//	ret[ovrdMat.first] = ovrdMat.second->UniqueName;
//	//}
//	//
//	//return ret;
//}

DirectX::XMFLOAT4X4 GRiSceneObject::GetTransform()
{
	return mWorld;
}

void GRiSceneObject::SetOverrideMaterial(std::string submeshName, GRiMaterial* mat)
{
	//if (pOverrideMaterial.find(submeshName) != pOverrideMaterial.end())
	//{
	if (mat == nullptr)
		pOverrideMaterial.erase(submeshName);
	else
		pOverrideMaterial[submeshName] = mat;
	//}
}

std::vector<float> GRiSceneObject::GetLocation()
{
	std::vector<float> ret = { Location[0], Location[1], Location[2] };
	return ret;
}

std::vector<float> GRiSceneObject::GetRotation()
{
	std::vector<float> ret = { Rotation[0], Rotation[1], Rotation[2] };
	return ret;
}

std::vector<float> GRiSceneObject::GetScale()
{
	std::vector<float> ret = { Scale[0], Scale[1], Scale[2] };
	return ret;
}

void GRiSceneObject::SetLocation(float x, float y, float z)
{
	Location[0] = x;
	Location[1] = y;
	Location[2] = z;
	bTransformDirty = true;
}

void GRiSceneObject::SetRotation(float pitch, float yaw, float roll)
{
	Rotation[0] = pitch;
	Rotation[1] = yaw;
	Rotation[2] = roll;
	bTransformDirty = true;
}

void GRiSceneObject::SetScale(float x, float y, float z)
{
	Scale[0] = x;
	Scale[1] = y;
	Scale[2] = z;
	bTransformDirty = true;
}

void GRiSceneObject::UpdateTransform()
{
	if (!bTransformDirty)
	{
		return;
	}
	auto t = DirectX::XMMatrixTranslation(Location[0], Location[1], Location[2]);
	auto r = DirectX::XMMatrixRotationRollPitchYaw(Rotation[0] * GGiEngineUtil::PI / 180.0f, Rotation[1] * GGiEngineUtil::PI / 180.0f, Rotation[2] * GGiEngineUtil::PI / 180.0f);
	auto s = DirectX::XMMatrixScaling(Scale[0], Scale[1], Scale[2]);
	auto transMat = DirectX::XMMatrixMultiply(DirectX::XMMatrixMultiply(s, r), t);
	//DirectX::XMFLOAT4X4 transfloat4x4;
	//DirectX::XMStoreFloat4x4(&transfloat4x4, transMat); 
	//GDxFloat4x4* trans = new GDxFloat4x4();
	//trans->SetValue(transfloat4x4);
	//std::shared_ptr<GGiFloat4x4> temp(trans);
	XMStoreFloat4x4(&mWorld,transMat);
	bTransformDirty = false;
}
